#include <stdio.h>
#include "unistd.h"
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "log.h"
/*日志文件的文件描述符*/
int logfile_fd = -1;

void logcmd(char *fmt,...)
{ /*一个变长参数的函数，即函数的参数是可变的，类似于printf 函数*/
	va_list ap;
/*关于va_list具体的用man stdarg 命令查看  */
	va_start(ap,fmt);  //whose number and type are unkown to  the called function
	// the called function must declare an object of  type va_list which is
	// used by Va_*() function.
	vprintf(fmt,ap);
	va_end(ap);
}

int init_logfile(char *filename)
{
	/*打开记录日志的文件*/
	logfile_fd = open(filename,O_RDWR|O_CREAT|O_APPEND,0666);
	if(logfile_fd < 0) {
		printf("open logfile failed\n");
		return -1;
	}

	return 0;
}

int logfile(char *file,int line,char *msg)
{
	/*将一条日志写入日志文件*/
	char buff[256];

	if(logfile_fd < 0)  return -1;
	snprintf(buff,256,"%s:%d %s\n",file,line,msg);  /*最多写入256个字符*/
	write(logfile_fd,buff,strlen(buff));
	
	return 0;
}
